package com.atguigu.tingshu.user.factory.imol;

import com.atguigu.tingshu.common.execption.GuiguException;
import com.atguigu.tingshu.user.factory.StrategyFactory;
import com.atguigu.tingshu.user.strategy.ItemTypeStrategy;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * @author: hy
 * @date:2025/9/17
 * @description:
 */
@Service
@Slf4j
public class StrategyFactoryImpl implements StrategyFactory {

    /**
     * 将IOC容器中ItemTypeStrategy接口下所有实现类对象注入到Map中
     * Map中Key：Bean对象ID
     * Map中Value：实现类对象
     * {"1001":购买类型为专辑策略类对象}
     * {"1002":购买类型为声音策略类对象}
     * {"1003":购买类型为会员策略类对象}
     */
    @Autowired
    private Map<String, ItemTypeStrategy> strategyMap;

    @Override
    public ItemTypeStrategy getStrategy(String key) {
        //获取到对应的策略类对象
        if (strategyMap.containsKey(key)) {
            //获取对应的策略类对象
            return strategyMap.get(key);
        }
        log.error("该策略实现类不存在");
        throw new GuiguException(500, "该策略" + key + "实现类不存在");
    }
}
